package edu.bbu.bibliospring.rsclient;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import edu.bbu.bibliospring.common.dto.AuthorDto;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;

import java.util.List;

/**
 * Created by anna on 6/15/14.
 * 10-es pont
 */
public class APITestClient {

    public static void main(String[] args) {

        Client client = new Client();
        String url;

        // POST insert new author
        AuthorDto authorDtoToInsert = new AuthorDto();
        authorDtoToInsert.setFirstName("Anna");
        authorDtoToInsert.setLastName("Deák");
        url = "http://localhost:8080/bibliospring-api/rest/authors/";
        AuthorDto insertedAuthorDto = executeOperation(client, url, "POST", AuthorDto.class, authorDtoToInsert);
        System.out.println("insertedAuthorDto = " + insertedAuthorDto);
        authorDtoToInsert.setFirstName("Jan");
        authorDtoToInsert.setLastName("Havelka");
        insertedAuthorDto = executeOperation(client, url, "POST", AuthorDto.class, authorDtoToInsert);
        System.out.println("insertedAuthorDto = " + insertedAuthorDto);
        authorDtoToInsert.setFirstName("Pista");
        authorDtoToInsert.setLastName("Hufnager");
        insertedAuthorDto = executeOperation(client, url, "POST", AuthorDto.class, authorDtoToInsert);
        System.out.println("insertedAuthorDto = " + insertedAuthorDto);

        // GET author by id
        url = "http://localhost:8080/bibliospring-api/rest/authors/1";
        AuthorDto getAuthorDtoByID = executeOperation(client, url, "GET", AuthorDto.class, null);
        System.out.println("getAuthorDtoByID = " + getAuthorDtoByID);

        // GET all authors
        ClientConfig cc = new DefaultClientConfig();
        cc.getClasses().add(JacksonJsonProvider.class);
        Client client2 = Client.create(cc);
        url = "http://localhost:8080/bibliospring-api/rest/authors/";
        WebResource webResource = client2.resource(url);
        WebResource.Builder builder = webResource.accept("application/json").type("application/json");
        ClientResponse clientResponse = (ClientResponse) builder.get(ClientResponse.class);

        List<AuthorDto> authorDtos = clientResponse.getEntity(new GenericType<List<AuthorDto>>(List.class));
//        Authors dtoList = clientResponse.getEntity(new GenericType<Authors>(Authors.class));
//        List<AuthorDto> authorDtos = dtoList.getAuthorDtoList();
        System.out.println("authorDtoList.size() = " + authorDtos.size());

    }

    public static <T> T executeOperation(Client client, String url, String method, Class<T> entityClass, AuthorDto authorDto) {
        WebResource webResource = client.resource(url);

        WebResource.Builder builder = webResource.accept("application/json").type("application/json");
        ClientResponse clientResponse = null;

        switch (method) {
            case "GET":
                clientResponse = (ClientResponse) builder.get(ClientResponse.class);
                break;
            case "POST":
                clientResponse = builder.entity(authorDto).post(ClientResponse.class);
                break;
        }
        if (clientResponse.getStatus() != 200) {
            throw new RuntimeException("Failed with HTTP error code" + clientResponse.getStatus());
        }
        return clientResponse.getEntity(entityClass);
    }
}
